#include <signal.h>
#include <unistd.h>
 #include <stdlib.h>
#include <string>
#include <fcntl.h>


const std::string default_load = "/";
const std::string default_file = "/dev/null";

void Daemon(int nochdir, int noclose)
{
     signal(SIGCHLD, SIG_IGN);
     signal(SIGPIPE, SIG_IGN);

     //会话进程不能是会话首进程
     if(fork() > 0)
     {
        exit(0);
     }

     setsid();
     if(nochdir == 0) chdir(default_load.c_str());
     if(noclose == 0)
     {
        int fd = open(default_file.c_str(), O_RDWR);
        if(fd > 0)
        {
            dup2(fd, 0);
            dup2(fd, 1);
            dup2(fd, 2);
            close(fd);
        }
        else
        {
            exit(1);
        }
     }
}